﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace luanvanFinal
{
    /// <summary>
    /// Interaction logic for WdReqChange.xaml
    /// </summary>
    public partial class WdReqChange : Window
    {
        int itemCount = 0;
        public WdReqChange()
        {
            InitializeComponent();
            loadItems();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            loadReqChange();

        }
        public void loadItems()
        {
            int count = 0;
            String strSQL = "select * from reqChange";
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            ketnoi.closeConnection();
            itemCount = dt.Rows.Count;
            if ((itemCount / 3) * 90 < 300)
            {
                cvs.Height = scr.Height-20;
            }
            else
                cvs.Height = (itemCount / 3) * 130;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < itemCount / 3; j++)
                {
                    TextBlock idchange = new TextBlock();
                    TextBlock idproject = new TextBlock();
                    Image img = new Image();
                    img.Source = new BitmapImage(new Uri(@"D:\image.png"));
                    StackPanel stackPnl = new StackPanel();
                    stackPnl.Orientation = Orientation.Horizontal;
                    TextBlock text = new TextBlock();
                    text.Text = "asdfd";
                    stackPnl.Children.Add(img);
                    clsItemsReqChange a = new clsItemsReqChange();
                    a.IdChange=dt.Rows[count][0].ToString();
                    a.IdProject = Convert.ToInt32(dt.Rows[count][2].ToString());
                    a.IdModifier = Convert.ToInt32(dt.Rows[count][1].ToString());                    
                    a.DateSubmitted = dt.Rows[count][3].ToString();
                    a.DateUpdate = dt.Rows[count][4].ToString();
                    a.Tittle = dt.Rows[count][5].ToString();
                    a.Originator = Convert.ToInt32(dt.Rows[count][11].ToString());
                    a.OriginatorPriority = dt.Rows[count][7].ToString();
                    a.ImpPriority = dt.Rows[count][8].ToString();
                    a.Status = dt.Rows[count][10].ToString();
                    a.Response =  dt.Rows[count][9].ToString();
                    a.Description = dt.Rows[count][6].ToString();
                    String strSQLa = "select proName from project where ID='"+a.IdProject+"'";
                    String strSQLb = "select useName from developer where id='" + a.IdModifier + "'";                    
                    ketnoi.openConnection();
                    SqlDataAdapter daa = new SqlDataAdapter(strSQLa, ketnoi.Connection);
                    SqlDataAdapter dab = new SqlDataAdapter(strSQLb, ketnoi.Connection);
                    DataTable dta = new DataTable();
                    daa.Fill(dta);
                    DataTable dtb = new DataTable();
                    daa.Fill(dtb);
                    ketnoi.closeConnection();
                    idchange.Text = a.IdChange;
                    idproject.Text = dta.Rows[0][0].ToString();
                    //a.Location = new System.Drawing.Point(i*50, j*50);
                    a.Height = 50; a.Width = 50;
                    a.Content = stackPnl;
                    a.SetValue(Canvas.LeftProperty, i * 80.0+10);
                    a.SetValue(Canvas.TopProperty,  j * 100.0);
                    idchange.SetValue(Canvas.LeftProperty, i * 80.0);
                    idchange.SetValue(Canvas.TopProperty, j * 100.0+50);
                    idproject.SetValue(Canvas.LeftProperty, i * 80.0);
                    idproject.SetValue(Canvas.TopProperty, j * 100.0 + 65);
                    cvs.Children.Add(a);
                    cvs.Children.Add(idchange);
                    cvs.Children.Add(idproject);
                    count++;
                    a.Click += a_Click;
                    
                }
            }

            for (int i = 0; i < itemCount % 3; i++)
            {
                TextBlock idchange = new TextBlock();
                TextBlock idproject = new TextBlock();
                Image img = new Image();
                img.Source = new BitmapImage(new Uri(@"D:\image.png"));
                StackPanel stackPnl = new StackPanel();
                stackPnl.Orientation = Orientation.Horizontal;
                TextBlock text = new TextBlock();
                text.Text = "asdfd";
                stackPnl.Children.Add(img);
                clsItemsReqChange a = new clsItemsReqChange();
                a.IdChange = dt.Rows[count][0].ToString();
                a.IdProject = Convert.ToInt32(dt.Rows[count][2].ToString());
                a.IdModifier = Convert.ToInt32(dt.Rows[count][1].ToString());
                a.DateSubmitted = dt.Rows[count][3].ToString();
                a.DateUpdate = dt.Rows[count][4].ToString();
                a.Tittle = dt.Rows[count][5].ToString();
                a.Originator = Convert.ToInt32(dt.Rows[count][11].ToString());
                a.OriginatorPriority = dt.Rows[count][7].ToString();
                a.ImpPriority = dt.Rows[count][8].ToString();
                a.Status = dt.Rows[count][10].ToString();
                a.Response = dt.Rows[count][9].ToString();
                a.Description = dt.Rows[count][6].ToString();
                String strSQLa = "select proName from project where ID='" + a.IdProject + "'";
                String strSQLb = "select useName from developer where id='" + a.IdModifier + "'";
                ketnoi.openConnection();
                SqlDataAdapter daa = new SqlDataAdapter(strSQLa, ketnoi.Connection);
                SqlDataAdapter dab = new SqlDataAdapter(strSQLb, ketnoi.Connection);
                DataTable dta = new DataTable();
                daa.Fill(dta);
                DataTable dtb = new DataTable();
                daa.Fill(dtb);
                ketnoi.closeConnection();
                idchange.Text = a.IdChange;
                idproject.Text = dta.Rows[0][0].ToString();
                //a.Location = new System.Drawing.Point(i*50, j*50);
                a.Height = 50; a.Width = 50;
                a.Content = stackPnl;
                a.SetValue(Canvas.LeftProperty, i * 80.0 + 10);
                a.SetValue(Canvas.TopProperty, (itemCount/3) * 100.0);
                idchange.SetValue(Canvas.LeftProperty, i * 80.0);
                idchange.SetValue(Canvas.TopProperty, (itemCount / 3) * 100.0 + 50);
                idproject.SetValue(Canvas.LeftProperty, i * 80.0);
                idproject.SetValue(Canvas.TopProperty, (itemCount / 3) * 100.0 + 65);
                cvs.Children.Add(a);
                cvs.Children.Add(idchange);
                cvs.Children.Add(idproject);
                count++;
                a.Click += a_Click;
            }
        }

        private void a_Click(object sender, EventArgs e)
        {
            clsConnect ketnoi = new clsConnect();
            clsItemsReqChange ab = sender as clsItemsReqChange;
            tbidChange.Text = ab.IdChange;
            String strSQLa = "select userName from developer where id='" + ab.IdModifier + "'";
            ketnoi.openConnection();
            SqlDataAdapter daa = new SqlDataAdapter(strSQLa, ketnoi.Connection);
            DataTable dta = new DataTable();
            daa.Fill(dta);
            ketnoi.closeConnection();
            String strSQLb = "select proName from project where ID='" + ab.IdProject+ "'";
            ketnoi.openConnection();
            SqlDataAdapter dab = new SqlDataAdapter(strSQLb, ketnoi.Connection);
            DataTable dtb = new DataTable();
            dab.Fill(dtb);
            ketnoi.closeConnection();
            String strSQLc = "select userName from customer where id='" + ab.Originator + "'";
            ketnoi.openConnection();
            SqlDataAdapter dac = new SqlDataAdapter(strSQLc, ketnoi.Connection);
            DataTable dtc = new DataTable();
            dac.Fill(dtc);
            ketnoi.closeConnection();
            cbProject.Text = dtb.Rows[0][0].ToString();
            cbModifier.Text = dta.Rows[0][0].ToString();
            dpDateSub.Text = ab.DateSubmitted.ToString();
            dpDateUpdate.Text = ab.DateUpdate.ToString();
            tbTittle.Text = ab.Tittle.ToString();
            cbOrigimator.Text = dtc.Rows[0][0].ToString();
            cbOriPri.Text = ab.OriginatorPriority.ToString();
            cbImpPri.Text = ab.ImpPriority.ToString();
            cbStatus.Text = ab.Status.ToString();
            Paragraph para = new Paragraph();
            para.Inlines.Add(new Run(ab.Description.ToString()));
            FlowDocument fd = new FlowDocument(para);
            rtbDes.Document = fd;
            Paragraph para1 = new Paragraph();
            para1.Inlines.Add(new Run(ab.Response.ToString()));
            FlowDocument fd1 = new FlowDocument(para1);
            rtbRes.Document = fd1;
        }

        public void loadReqChange()
        {
            String strSQL = "select proName from project";
            String strSQLDev = "select userName from developer";
            String strSQLCus = "select userName from customer";
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbProject.Items.Add(dt.Rows[i][0]);
            }
            cbProject.SelectedIndex = 0;
            SqlDataAdapter daDev = new SqlDataAdapter(strSQLDev, ketnoi.Connection);
            DataTable dtDev = new DataTable();
            daDev.Fill(dtDev);
            for (int i = 0; i < dtDev.Rows.Count; i++)
            {
                cbModifier.Items.Add(dtDev.Rows[i][0]);
            }
            cbModifier.SelectedIndex = 0;
            SqlDataAdapter daCus = new SqlDataAdapter(strSQLCus, ketnoi.Connection);
            DataTable dtCus = new DataTable();
            daCus.Fill(dtCus);
            for (int i = 0; i < dtCus.Rows.Count; i++)
            {
                cbOrigimator.Items.Add(dtCus.Rows[i][0]);
            }
            cbOrigimator.SelectedIndex = 0;
            ketnoi.closeConnection();

        }

        public void insertReqChange()
        {
            int idModifier;
            int idOriginator;
            clsProject project = new clsProject();
            clsDeveloper dev = new clsDeveloper();
            clsCustomer cus = new clsCustomer();
            clsReqChange reqChange = new clsReqChange();
            reqChange.IdChange = tbidChange.Text;
            String idDevNames = dev.getIdFromName(cbModifier.Text);
            int.TryParse(idDevNames, out idModifier);
            reqChange.IdModifier = idModifier;
            String idProNames = project.getIdFromName(cbProject.Text); //lay id tu project name
            int idProName;
            int.TryParse(idProNames, out idProName);
            reqChange.IdProject=idProName;
            reqChange.DateSubmitted=dpDateSub.SelectedDate;
            reqChange.DateUpdate = dpDateUpdate.SelectedDate;
            reqChange.Tittle=tbTittle.Text;
            reqChange.Description = new TextRange(rtbDes.Document.ContentStart, rtbDes.Document.ContentEnd).Text;
            reqChange.OriginatorPriority = cbOriPri.Text;
            reqChange.Response = new TextRange(rtbRes.Document.ContentStart, rtbRes.Document.ContentEnd).Text;
            reqChange.Status = cbStatus.Text;
            String idCusNames = cus.getIdFromName(cbOrigimator.Text);
            int.TryParse(idCusNames, out idOriginator);
            reqChange.Originator = idOriginator;
            bool kt = true;
            String strSQL = "select idChange,idProject from reqChange";
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (reqChange.IdChange == dt.Rows[i][0].ToString() && reqChange.IdProject == Convert.ToInt32(dt.Rows[i][1]))
                    kt = false;
            }
            ketnoi.closeConnection();
            if (reqChange.IdChange == "")
                MessageBox.Show("Requirement Change Id can not null", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            else if (reqChange.Tittle == "")
                MessageBox.Show("Requirement Change  tittle can not null !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            else if (kt == false)
                MessageBox.Show("Id Requirement change and Project can not valid", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            else
            {
                reqChange.insertReqChange();
                MessageBox.Show("You have insert success !!!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
            } 
        }

        public void updateReqChange()
        {
            int idModifier;
            int idOriginator;
            clsProject project = new clsProject();
            clsCustomer cus = new clsCustomer();
            clsDeveloper dev = new clsDeveloper();
            clsReqChange reqChange = new clsReqChange();
            reqChange.IdChange = tbidChange.Text;
            String idDevNames = dev.getIdFromName(cbModifier.Text);
            int.TryParse(idDevNames, out idModifier);
            reqChange.IdModifier = idModifier;
            String idProNames = project.getIdFromName(cbProject.Text); //lay id tu project name
            int idProName;
            int.TryParse(idProNames, out idProName);
            reqChange.IdProject = idProName;
            reqChange.DateSubmitted = dpDateSub.SelectedDate;
            reqChange.DateUpdate = dpDateUpdate.SelectedDate;
            reqChange.Tittle = tbTittle.Text;
            reqChange.Description = new TextRange(rtbDes.Document.ContentStart, rtbDes.Document.ContentEnd).Text;
            reqChange.OriginatorPriority = cbOriPri.Text;
            reqChange.Response = new TextRange(rtbRes.Document.ContentStart, rtbRes.Document.ContentEnd).Text;
            reqChange.Status = cbStatus.Text;
            String idCusNames = cus.getIdFromName(cbOrigimator.Text);
            int.TryParse(idCusNames, out idOriginator);
            reqChange.Originator = idOriginator;
            bool kt = true;
            String strSQL = "select idChange,idProject from reqChange";
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (reqChange.IdChange == dt.Rows[i][0].ToString() && reqChange.IdProject == Convert.ToInt32(dt.Rows[i][1]))
                    kt = false;
            }
            ketnoi.closeConnection();
            if (reqChange.IdChange == "")
                MessageBox.Show("Requirement Change Id can not null", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            else if (reqChange.Tittle == "")
                MessageBox.Show("Requirement Change  tittle can not null !!!", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            else if (kt == true)
                MessageBox.Show("Id Requirement change and Project can not valid", "Warrning", MessageBoxButton.OK, MessageBoxImage.Stop);
            else
            {
                reqChange.updateReqChange();
                MessageBox.Show("You have update success !!!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        public void deleteReqChange()
        {
            try
            {

                clsReqChange reqChange = new clsReqChange();
                clsConnect connect = new clsConnect();
                clsProject project = new clsProject();
                String idProNames = project.getIdFromName(cbProject.Text); //lay id tu project name
                int idProName;
                int.TryParse(idProNames, out idProName);
                //MessageBox.Show(ID);
                reqChange.IdChange = tbidChange.Text;
                reqChange.IdProject = idProName;
                reqChange.deleteReqChange();
                connect.openConnection();
            }
            catch
            {
                MessageBox.Show("Error while deleting", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            insertReqChange();
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            updateReqChange();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            deleteReqChange();
        }
    }
}
